package ve.usb.ddp;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import ve.usb.cohesion.runtime.CohesionAction;

import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction;
import ve.usb.cohesion.runtime.HibernateUtil;
import ve.usb.ddp.hibernate.*;


/**
 * Manejo de elementos de la entidad PaiEstudio.<br>
 */
public class AccionesPaiEstudio extends CohesionAction {
    
/**
     * Called by Struts for the execution of action APreBuscar_paiEstudio.
     * Prepara la vista de selección de criterios de búsqueda para una entidad.<br>
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward APreBuscar_paiEstudio(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        //Salidas
        final String[] SALIDAS = {"VBuscar_paiEstudio", };
        final int SALIDA_0 = 0;

        int salida = SALIDA_0;
//Checking for actors administrador
            if (!CohesionActor.checkActor(request, 1)) {
                return mapping.findForward(CohesionActor.SALIDA_ACTOR);
            }
        Session s = HibernateUtil.getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {
            FPaiEstudioBuscar.clear(mapping, request);

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        } finally {
            try { s.close(); } catch (Exception ex2) {}
        }
        return mapping.findForward(SALIDAS[salida]);
    }

/**
     * Called by Struts for the execution of action APreCrear_paiEstudio.
     * Prepara la vista de captura de datos de una entidad para realizar una inserción.<br>
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward APreCrear_paiEstudio(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        //Salidas
        final String[] SALIDAS = {"VCrear_paiEstudio", };
        final int SALIDA_0 = 0;

        int salida = SALIDA_0;
//Checking for actors administrador
            if (!CohesionActor.checkActor(request, 1)) {
                return mapping.findForward(CohesionActor.SALIDA_ACTOR);
            }
        Session s = HibernateUtil.getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {
            FPaiEstudio fFPaiEstudio = FPaiEstudio.clear(mapping, request);

             //Feed for combo listPaiResolucion.
             {java.util.List<ve.usb.cohesion.runtime.Pair> list = 
                (java.util.List<ve.usb.cohesion.runtime.Pair>)s.createQuery(
                "select new ve.usb.cohesion.runtime.Pair(fec_Resolucion, idResolucion) " + 
                "from PaiResolucion").list();
              list.add(0,new ve.usb.cohesion.runtime.Pair("Ninguna", 0));
              fFPaiEstudio.setListPaiResolucion(list);
             }


            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        } finally {
            try { s.close(); } catch (Exception ex2) {}
        }
        return mapping.findForward(SALIDAS[salida]);
    }

/**
     * Called by Struts for the execution of action ABuscar_paiEstudio.
     * Captura los criterios de búsqueda y los fija en la sesión.<br>
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward ABuscar_paiEstudio(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        //Salidas
        final String[] SALIDAS = {"APreListar_paiEstudio", };
        final int SALIDA_0 = 0;

        int salida = SALIDA_0;
//Checking for actors administrador
            if (!CohesionActor.checkActor(request, 1)) {
                return mapping.findForward(CohesionActor.SALIDA_ACTOR);
            }
        Session s = HibernateUtil.getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {
            FPaiEstudioBuscar fFPaiEstudioBuscar = (FPaiEstudioBuscar)form;
            request.getSession().setAttribute("ABuscar_paiEstudio_campo", "pais");
            request.getSession().setAttribute("ABuscar_paiEstudio_valor", "%" + fFPaiEstudioBuscar.getPais() + "%");

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        } finally {
            try { s.close(); } catch (Exception ex2) {}
        }
        return mapping.findForward(SALIDAS[salida]);
    }

/**
     * Called by Struts for the execution of action APreListar_paiEstudio.
     * Selecciona las entidades a ser listadas de acuerdo a los parámetros recibidos.<br>
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward APreListar_paiEstudio(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        //Salidas
        final String[] SALIDAS = {"VListar_paiEstudio", };
        final int SALIDA_0 = 0;

        int salida = SALIDA_0;
//Checking for actors administrador
            if (!CohesionActor.checkActor(request, 1)) {
                return mapping.findForward(CohesionActor.SALIDA_ACTOR);
            }
        Session s = HibernateUtil.getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {
            String crit = (String)request.getSession().getAttribute("ABuscar_paiEstudio_campo");
            java.util.List<PaiEstudio> list;
            if (crit==null) {
                list = (java.util.List<PaiEstudio>)s.createQuery("from PaiEstudio").list();
            } else {
                String valor = (String)request.getSession().getAttribute("ABuscar_paiEstudio_valor");
                list = (java.util.List<PaiEstudio>)
                   s.createQuery("from PaiEstudio where "+crit+" like :valor")
                    .setString("valor", valor).list();
                request.getSession().removeAttribute("ABuscar_paiEstudio_campo");
                request.getSession().removeAttribute("ABuscar_paiEstudio_valor");
            }
            request.setAttribute("L_PaiEstudio", list);
            for (PaiEstudio paiEstudio:list) {
                if (paiEstudio.getResolucion()!=null) paiEstudio.getResolucion().getFec_Resolucion();
            }


            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        } finally {
            try { s.close(); } catch (Exception ex2) {}
        }
        return mapping.findForward(SALIDAS[salida]);
    }

/**
     * Called by Struts for the execution of action APre_paiEstudio.
     * Prepara la vista de actualización de una entidad.<br>
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward APre_paiEstudio(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        //Salidas
        final String[] SALIDAS = {"V_paiEstudio", };
        final int SALIDA_0 = 0;

        int salida = SALIDA_0;
//Checking for actors administrador
            if (!CohesionActor.checkActor(request, 1)) {
                return mapping.findForward(CohesionActor.SALIDA_ACTOR);
            }
        Session s = HibernateUtil.getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {
            Long idEstudio = lookup_Long(request, "idEstudio");
            
            FPaiEstudio fFPaiEstudio = FPaiEstudio.clear(mapping, request);
            PaiEstudio paiEstudio = (PaiEstudio)s.load(PaiEstudio.class, idEstudio);
            fFPaiEstudio.setIdEstudio(paiEstudio.getIdEstudio());
            if (paiEstudio.getResolucion()!=null) {
                fFPaiEstudio.setResolucion(paiEstudio.getResolucion().getIdResolucion());
            } else {
                fFPaiEstudio.setResolucion(0L);
            }
            fFPaiEstudio.setPais(paiEstudio.getPais());
            fFPaiEstudio.setUniversidad(paiEstudio.getUniversidad());
            fFPaiEstudio.setNivel(paiEstudio.getNivel());
            fFPaiEstudio.setModalidad(paiEstudio.getModalidad());
            fFPaiEstudio.setFec_Ini(paiEstudio.getFec_Ini().toString());
            fFPaiEstudio.setFec_Fin(paiEstudio.getFec_Fin().toString());

            //Feed for combo listPaiResolucion.
            {java.util.List<ve.usb.cohesion.runtime.Pair> list = 
               (java.util.List<ve.usb.cohesion.runtime.Pair>)s.createQuery(
               "select new ve.usb.cohesion.runtime.Pair(fec_Resolucion, idResolucion) " + 
               "from PaiResolucion").list();
             list.add(0,new ve.usb.cohesion.runtime.Pair("Ninguna", 0));
             fFPaiEstudio.setListPaiResolucion(list);
            }

            request.getSession().setAttribute("idEstudio", idEstudio);
            request.getSession().setAttribute("idEstudio_super", idEstudio);

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        } finally {
            try { s.close(); } catch (Exception ex2) {}
        }
        return mapping.findForward(SALIDAS[salida]);
    }

/**
     * Called by Struts for the execution of action AAct_paiEstudio.
     * Registro de actualizaciones de una entidad.<br>
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward AAct_paiEstudio(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        //Salidas
        final String[] SALIDAS = {"V_paiEstudio", };
        final int SALIDA_0 = 0;

        int salida = SALIDA_0;
//Checking for actors administrador
            if (!CohesionActor.checkActor(request, 1)) {
                return mapping.findForward(CohesionActor.SALIDA_ACTOR);
            }
        Session s = HibernateUtil.getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {
            FPaiEstudio fFPaiEstudio = (FPaiEstudio)form;
            PaiEstudio paiEstudio = (PaiEstudio)s.load(PaiEstudio.class, fFPaiEstudio.getIdEstudio());
            if (fFPaiEstudio.getResolucion()==0) paiEstudio.setResolucion(null);
            else if ((paiEstudio.getResolucion()==null) ||
                    (paiEstudio.getResolucion().getIdResolucion()!=fFPaiEstudio.getResolucion())) {
                PaiResolucion __obj = (PaiResolucion)s.load(PaiResolucion.class, fFPaiEstudio.getResolucion());
                paiEstudio.setResolucion(__obj);
            }
            paiEstudio.setPais(fFPaiEstudio.getPais());
            paiEstudio.setUniversidad(fFPaiEstudio.getUniversidad());
            paiEstudio.setNivel(fFPaiEstudio.getNivel());
            paiEstudio.setModalidad(fFPaiEstudio.getModalidad());
            paiEstudio.setFec_Ini(new ve.usb.cohesion.runtime.LocalDate(fFPaiEstudio.getFec_Ini()));
            paiEstudio.setFec_Fin(new ve.usb.cohesion.runtime.LocalDate(fFPaiEstudio.getFec_Fin()));


            //Feed for combo listPaiResolucion.
            {java.util.List<ve.usb.cohesion.runtime.Pair> list = 
               (java.util.List<ve.usb.cohesion.runtime.Pair>)s.createQuery(
               "select new ve.usb.cohesion.runtime.Pair(fec_Resolucion, idResolucion) " + 
               "from PaiResolucion").list();
             list.add(0,new ve.usb.cohesion.runtime.Pair("Ninguna", 0));
             fFPaiEstudio.setListPaiResolucion(list);
            }

            request.getSession().setAttribute("idEstudio", paiEstudio.getIdEstudio());
            request.getSession().setAttribute("idEstudio_super", paiEstudio.getIdEstudio());

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        } finally {
            try { s.close(); } catch (Exception ex2) {}
        }
        if (salida==0) {
          request.setAttribute("msg",
            getResources(request).getMessage("AAct_paiEstudio.msg0"));
        }

        return mapping.findForward(SALIDAS[salida]);
    }

/**
     * Called by Struts for the execution of action AElim_paiEstudio.
     * Elimina la entidad seleccionada.<br>
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward AElim_paiEstudio(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        //Salidas
        final String[] SALIDAS = {"APreListar_paiEstudio", "V_paiEstudio", };
        final int SALIDA_0 = 0;
        final int SALIDA_1 = 1;

        int salida = SALIDA_0;
//Checking for actors administrador
            if (!CohesionActor.checkActor(request, 1)) {
                return mapping.findForward(CohesionActor.SALIDA_ACTOR);
            }
        Session s = HibernateUtil.getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {
            Long idEstudio = lookup_Long(request, "idEstudio");
            PaiEstudio paiEstudio = (PaiEstudio)s.load(PaiEstudio.class, idEstudio);
            s.delete(paiEstudio);
            try {
                s.flush();
            } catch (Exception ex1) {
                ex1.printStackTrace();
                salida = SALIDA_1;
            }

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        } finally {
            try { s.close(); } catch (Exception ex2) {}
        }
        if (salida==0) {
          request.setAttribute("msg",
            getResources(request).getMessage("AElim_paiEstudio.msg0"));
        }
        if (salida==1) {
          request.setAttribute("msg",
            getResources(request).getMessage("AElim_paiEstudio.msg1"));
        }

        return mapping.findForward(SALIDAS[salida]);
    }

/**
     * Called by Struts for the execution of action ACrear_paiEstudio.
     * Registro de la creación de una PaiEstudio.<br>
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward ACrear_paiEstudio(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        //Salidas
        final String[] SALIDAS = {"V_paiEstudio", };
        final int SALIDA_0 = 0;

        int salida = SALIDA_0;
//Checking for actors administrador
            if (!CohesionActor.checkActor(request, 1)) {
                return mapping.findForward(CohesionActor.SALIDA_ACTOR);
            }
        Session s = HibernateUtil.getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {
            FPaiEstudio fFPaiEstudio = (FPaiEstudio)form;
            PaiEstudio paiEstudio = new PaiEstudio();
            if (fFPaiEstudio.getResolucion()==0) paiEstudio.setResolucion(null);
            else {
                PaiResolucion __obj = (PaiResolucion)s.load(PaiResolucion.class, fFPaiEstudio.getResolucion());
                paiEstudio.setResolucion(__obj);
            }
            paiEstudio.setPais(fFPaiEstudio.getPais());
            paiEstudio.setUniversidad(fFPaiEstudio.getUniversidad());
            paiEstudio.setNivel(fFPaiEstudio.getNivel());
            paiEstudio.setModalidad(fFPaiEstudio.getModalidad());
            paiEstudio.setFec_Ini(new ve.usb.cohesion.runtime.LocalDate(fFPaiEstudio.getFec_Ini()));
            paiEstudio.setFec_Fin(new ve.usb.cohesion.runtime.LocalDate(fFPaiEstudio.getFec_Fin()));

            s.save(paiEstudio);
            s.flush();
            fFPaiEstudio.setIdEstudio(paiEstudio.getIdEstudio());

            //Feed for combo listPaiResolucion.
            {java.util.List<ve.usb.cohesion.runtime.Pair> list = 
               (java.util.List<ve.usb.cohesion.runtime.Pair>)s.createQuery(
               "select new ve.usb.cohesion.runtime.Pair(fec_Resolucion, idResolucion) " + 
               "from PaiResolucion").list();
             list.add(0,new ve.usb.cohesion.runtime.Pair("Ninguna", 0));
             fFPaiEstudio.setListPaiResolucion(list);
            }

            request.getSession().setAttribute("idEstudio", paiEstudio.getIdEstudio());
            request.getSession().setAttribute("idEstudio_super", paiEstudio.getIdEstudio());

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        } finally {
            try { s.close(); } catch (Exception ex2) {}
        }
        if (salida==0) {
          request.setAttribute("msg",
            getResources(request).getMessage("ACrear_paiEstudio.msg0"));
        }

        return mapping.findForward(SALIDAS[salida]);
    }



}